package cn.net.isir.service2;

import cn.net.isir.common.ResultObject;
import cn.net.isir.common.TokenManager;
import cn.net.isir.core.BasicService2;
import cn.net.isir.core.InsertRequired;
import cn.net.isir.core.SetBasicDao;
import cn.net.isir.core.UpdateRequired;
import cn.net.isir.dao.ShopDao;
import cn.net.isir.dao.ShopUserDao;
import cn.net.isir.entity.Shop;
import cn.net.isir.entity.ShopUser;
import cn.net.isir.utils.RequestUtils;
import cn.net.isir.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopService extends BasicService2<Shop> {
    @SetBasicDao
    @Resource
    private ShopDao shopDao;
    @Resource
    private ShopUserDao shopUserDao;

    public int insert2(Shop shop) {
        shopDao.insert(shop);
        shopDao.insert(shop);
        return 0;
    }

    /**
     * 新增用戶
     *
     * @param shop 店铺信息
     * @return 新增结果
     */
    @Override
    public int insert(Shop shop) {
        //检查基本信息
        ResultObject ro = RequestUtils.checkField(shop, InsertRequired.class);
        if (ro.getResult() != 200) {
            return 0;
        }
        if (shop == null) {
            return 0;
        }
        if (StringUtils.isBlank(shop.getName())) {
            return 0;
        }
        String subAccount = shop.getSubAccount();
//        shop.setName(name);
//        shop.setProvince(province);
//        shop.setCity(city);
//        shop.setArea(area);
//        shop.setAddress(address);
//        shop.setBusinessHours(businessHours);
//        shop.setOrderStyle(orderStyle);//table.serial
//        shop.setContact(contact);
//        shop.setTel(tel);
//        shop.setIcon(icon);
        //shop.setMerchantId(merchantId);
        //shop.setAlipayMerchantId(alipayMerchantId);
        //shop.setWxpayMerchantId(wxpayMerchantId);
        //shop.setAlipayBusinessInfo(alipayBusinessInfo);
        //shop.setWxpayBusinessInfo(wxpayBusinessInfo);
//        shop.setSalesmanCode(salesmanCode);//销售顾问
        shop.setCreateUser(TokenManager.getTokenId());
        shop.setAlipayAuthStatus("0");//新店铺，默认未授权
        shop.setAlipaySignStatus("0");//新店铺，默认未签约
        shopDao.insert(shop);
        //新增商户的用户店铺关联表
        ShopUser shopUser = new ShopUser();
        shopUser.setUserId(TokenManager.getTokenId());
//        shopUser.setBusinessId();
        shopUser.setShopId(shop.getId());
        shopUser.setIsSubAccount(0);
        shopUser.setCreateUser(TokenManager.getTokenId());
        shopUserDao.insert(shopUser);
        //更新子账户的店铺关联表(将新增的用户关联信息中，增加店铺id)
        if (StringUtils.isNotBlank(subAccount)) {
            for (String id : subAccount.split(",")) {
                ShopUser subUser = new ShopUser();
                subUser.setUserId(id);
                subUser = shopUserDao.selectOne(subUser);
                subUser.setShopId(shop.getId());
                shopUserDao.update(subUser);
            }
        }
        return 1;
    }

    /**
     * 更新信息
     *
     * @param shop 店鋪信息
     * @return 结果
     */
    @Override
    public int update(Shop shop) {
        //检查基本信息
        ResultObject ro = RequestUtils.checkField(shop, UpdateRequired.class);
        if (ro.getResult() != 200) {
            return 0;
        }
        //更新店铺基本信息
        shopDao.update(shop);
        //更新子账户的店铺关联表(将新增的用户关联信息中，增加店铺id)
        String subAccount = shop.getSubAccount();
        if (StringUtils.isNotBlank(subAccount)) {
            for (String id : subAccount.split(",")) {
                ShopUser subUser = new ShopUser();
                subUser.setUserId(id);
                subUser = shopUserDao.selectOne(subUser);
                subUser.setShopId(shop.getId());
                shopUserDao.update(subUser);
            }
        }

        return 1;
    }

    @Override
    public int delete(String id) {
        super.delete(id);
        ShopUser shopUser = new ShopUser();
        shopUser.setShopId(id);
        List<ShopUser> shopUserList = shopUserDao.selectList(shopUser);
        for (ShopUser su : shopUserList) {
            shopUserDao.delete(su.getId());
        }
        return 1;
    }

    /**
     * 根据销售人员ID,查询店铺列表
     *
     * @param salesIds 销售人员ID
     * @param pageNo   页码
     * @param pageSize 分页大小
     * @return 分页
     */
    public PageInfo selectShopListByUserIds(String salesIds, String saleId, String name, Date startTime1, Date endTime1, Integer pageNo, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        String startTime =null;
        String endTime =null;
        if (startTime1 != null && endTime1 != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            startTime = sdf.format(startTime1);
            endTime = sdf.format(endTime1);
        }
        map.put("salesIds", salesIds);
        map.put("salesmanId", saleId);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("name", name);
        return shopDao.selectPageList(map, "selectShopListByUserIds", pageNo, pageSize);
    }
}
